package com.qcxk.yuqian.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcxk.yuqian.system.component.behaviour.BehaviourOperator;
import com.qcxk.yuqian.system.component.constant.SystemConstant;
import com.qcxk.yuqian.system.entity.Dictionary;
import com.qcxk.yuqian.system.service.DictionaryService;
import com.qcxk.yuqian.system.utils.ThmsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/dic")
public class DictionaryController {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private DictionaryService dictionaryService;

    @BehaviourOperator(desc = "查询字典列表")
    @RequestMapping(value = "/queryDicList")
    public JSONObject queryDicList(Page page, Dictionary dictionary) {
        JSONObject json = new JSONObject();
        page = dictionaryService.queryDictionaryPage(page, dictionary);
        json.put("code", "0");
        json.put("current", page.getCurrent());
        json.put("size", page.getSize());
        json.put("total", page.getTotal());
        json.put("dataList", page.getRecords());
        return json;
    }

    @BehaviourOperator(desc = "查询字典")
    @RequestMapping(value = "/queryDics")
    public JSONObject queryDics(Dictionary dictionary) {
        JSONObject json = new JSONObject();
        QueryWrapper<Dictionary> dictionaryQueryWrapper = new QueryWrapper<>(dictionary);
        List<Dictionary> dictionaryList = dictionaryService.list(dictionaryQueryWrapper);
        json.put("code", "0");
        //针对各类型不同处理
        json.put("dataList", dictionaryList);
        return json;
    }

    @BehaviourOperator(desc = "查询字典项")
    @RequestMapping(value = "/loadDic")
    public JSONObject loadDic(Dictionary dictionary) {
        JSONObject json = new JSONObject();
        dictionary = dictionaryService.getById(dictionary.getId());
        if (dictionary != null) {
            json.put("code", "0");
            JSONObject jsonObject = new JSONObject();
            json.put("data", jsonObject);
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

    @BehaviourOperator(desc = "查询字典信息")
    @RequestMapping(value = "/loadDicForAdmin")
    public JSONObject loadDicForAdmin(Dictionary dictionary) {
        JSONObject json = new JSONObject();
        dictionary = dictionaryService.getById(dictionary.getId());
        if (dictionary != null) {
            json.put("code", "0");
            json.put("data", dictionary);
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

    @BehaviourOperator(desc = "增加字典项")
    @RequestMapping(value = "/addDic")
    public JSONObject addDic(Dictionary dictionary) {
        JSONObject json = new JSONObject();
        dictionary.setCtime(ThmsUtil.getDate());
        if (dictionaryService.save(dictionary)) {
            json.put("code", "0");
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

    @BehaviourOperator(desc = "修改字典项")
    @RequestMapping(value = "/updateDic")
    public JSONObject updateDic(Dictionary dictionary) {
        JSONObject json = new JSONObject();
        if (dictionaryService.updateDictionary(dictionary)) {
            json.put("code", "0");
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

    @BehaviourOperator(desc = "删除字典项")
    @RequestMapping(value = "/delDic")
    public JSONObject delDic(Dictionary dictionary) {
        JSONObject json = new JSONObject();
        if (dictionaryService.deleteDictionaryById(dictionary)) {
            json.put("code", "0");
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }
}
